$(function(){
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dom: {
            frame: null,
            area: null,
            school: null,
            start: null,
            end: null
        },
        params: {
            schoolId: null,
            startTime: null,
            endTime: null
        },
        timer: null,
        init: function () {
            this.params.schoolId = xhTool.getQuery('schoolId');

            this.dom.frame   = $('iframe');
            this.dom.school  = $('#school_id');
            this.dom.area    = $('#school_area');
            this.dom.start   = $('#startTime');
            this.dom.end     = $('#endTime');
            this.dom.btn     = $('#btn_view');

            if (this.params.schoolId) {
                this.dom.school.add(this.dom.area).parent().remove();
                this.dom.school = null;
                this.dom.area   = null;
            }

            this.listener();
        },
        listener: function () {
            var $this = this;
            //点击查询
            $this.dom.btn.click(function () {
                $this.funObj().viewUsage();
            });
            $this.dom.frame.load(function(){
                xhExtra.setBtnNormal($this.dom.btn);
            });
            //绑定区域
            $this.funObj().bindAreaSchool();
        },
        funObj: function(){
            var $this = this;
            return {
                viewUsage: function () {
                    $this.dom.frame.attr('src', '');
                    if ($this.dom.school) {
                        $this.params.schoolId = $this.dom.school.val();
                    }
                    $this.params.startTime  = $this.dom.start.val();
                    $this.params.endTime    = $this.dom.end.val();
                    if (!$this.params.schoolId) {
                        xhTop.showError('学校为必选项！');
                        return false;
                    }
                    if (!$this.params.startTime || !$this.params.endTime) {
                        xhTop.showError('时间不能为空！');
                        return false;
                    }
                    $this.dom.frame.attr('src', baseUrl + 'Statsschool/tchBookInfo?' + xhTool.parseParam($this.params));
                },
                bindAreaSchool: function () {
                    if ($this.dom.area.length >= 1) {
                        xhExtra.ajaxRequest('Other/getDep', {tag: 'XHYYB', depth: 2}, {check: true}, function (data) {
                            $this.dom.area.select2({data: data}).change(function () {
                                xhExtra.ajaxRequest('Other/getSchoolByDep', {depId: $(this).val()}, {check: false}, function (schools) {
                                    if (xhExtra.checkError(schools)) {
                                        $this.dom.school.empty();
                                        return false;
                                    }
                                    $this.dom.school.empty().select2({data: schools});
                                });
                            }).trigger('change');
                        });
                    }
                    else {
                        $this.dom.school.select2();
                    }
                }
            }
        }
    }
}